<!--  -->
<template>
  <div class='authority'>
    <el-button type="primary" v-permissions="'switchGateway:add'" @click="addEatTagNow" style="margin-bottom: 10px;">
      新增
    </el-button>
    <!-- 查询 -->
    <el-form :inline="true" :model="FindFromForEarTag" class="demo-form-inline">
      <el-form-item label="耳标编号">
        <el-input v-model="FindFromForEarTag.kw" placeholder="请输入耳标"></el-input>
      </el-form-item>
      <el-form-item label="网关">
        <el-select v-model="FindFromForEarTag.gatewayId" placeholder="请选择网关">
          <el-option
            v-for="(item,index) in FindFromForEarTag.gatewayIdArr"
            :key="index"
            :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitSrearch">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <!-- 列表展示 -->
      <el-table
        :data="getGaleWayList"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="设备编号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="iHi"
          label="电流高阈值"
          width="80">
        </el-table-column>
        <el-table-column
          prop="iMut"
          label="电流突变"
          width="80">
        </el-table-column>
        <el-table-column
          prop="tHi"
          label="温度高阈值"
          width="160">
        </el-table-column>
        <el-table-column
          prop="tMut"
          label="温度突变"
          width="160">
        </el-table-column>
        <el-table-column
          prop="baud"
          label="485 波特率"
          width="160">
        </el-table-column>
        <el-table-column
          prop="alm"
          label="报警发生"
          width="160">
        </el-table-column>
        <el-table-column
          prop="eMut"
          label="用电量"
          width="160">
        </el-table-column>
        <el-table-column
          prop="mode"
          label="工作模式"
          width="160">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          >
          <template slot-scope="scope">
            <el-button @click="getLiveSwitchDetail(scope.row.id)" type="text" size="small">详情</el-button>
            <!-- <el-button @click="deletLiveStockDUser(scope.row.id)" type="text" size="small">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog
        title="耳标详情"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :show-close="false"
        :close-on-press-escape="false"
        width="50%">
        <el-form
          ref="deatilsFrom"
          :model="updateSwitchDetail"
          :disabled="isDisabled"
          :inline="false"
          class="demo-form-inline"
          label-position="left"
          label-width="auto"
          :rules="updateEarTagRules">
          <div class="filter-container">
            <el-form-item label="电流高阈值" prop="iHi">
              <el-input v-model="updateSwitchDetail.iHi"></el-input>
            </el-form-item>
            <el-form-item label="电流突变" prop="iMut">
              <el-input v-model="updateSwitchDetail.iMut"></el-input>
            </el-form-item>
            <el-form-item label="温度高阈值" prop="tHi">
              <el-input v-model="updateSwitchDetail.tHi"></el-input>
            </el-form-item>
            <el-form-item label="485 波特率" prop="baud">
              <el-input v-model="updateSwitchDetail.baud"></el-input>
            </el-form-item>
            <el-form-item label="报警发生" prop="alm">
              <el-input v-model="updateSwitchDetail.alm"></el-input>
            </el-form-item>
            <el-form-item label="用电量" prop="eMut">
              <el-input v-model="updateSwitchDetail.eMut"></el-input>
            </el-form-item>
            <el-form-item label="工作模式" prop="mode">
              <el-input v-model="updateSwitchDetail.mode"></el-input>
            </el-form-item>
          </div>
        </el-form>
        <div class="buttons">
           <!-- <el-button type="primary" @click="setConfigUserRole" v-if="isDisabled">修改</el-button>
           <el-button type="primary" @click="submitConfigUser" v-if="!isDisabled">保存</el-button> -->
           <el-button @click="closeDiaDetail">关闭</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="新增开关网关"
        :visible.sync="addNewSwitchData"
        @close="closeDia"
        width="50%">
        <el-form ref="formName" :model="formForAddSwitchGateway" :rules="addEarTagRule" label-width="80px">
          <el-form-item label="设备编号" prop="num">
            <el-input v-model="formForAddSwitchGateway.num"></el-input>
          </el-form-item>
        </el-form>
        <div style="width: 100%; text-align: center;">
          <el-button type="primary" @click="commitSwitch">保存</el-button>
        </div>
      </el-dialog>
    </div>
    <pagination
      class="pagination_container"
      :total="pagination.total"
      :page-sizes="[10, 20, 50, 100]"
      :page.sync="pagination.page"
      :limit.sync="pagination.pageSize"
      @pagination="getEarTagListing"
    />
  </div>
</template>

<script>
import { switchGatewayAdd, switchGatewayList, switchGatewayDetail } from '@/api/switchGatewayList'
import Pagination from '@/components/pagination'
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    Pagination
  },
  data () {
    // 这里存放数据
    return {
      isDisabled: true,
      updateSwitchDetail: { // 修改品种
        gatewayName: '',
        num: '',
        gatewayId: '',
        gatewayIdArr: []
      },
      updateEarTagRules: {
        num: [
          { required: true, trigger: 'blur', message: '请输入耳标编号' }
        ],
        gatewayId: [
          { required: true, trigger: 'change', message: '请选择网关' }
        ]
      },
      getGaleWayList: [], // 牧场列表
      formForAddSwitchGateway: {
        num: ''
      },
      // 新增 rule 验证
      addEarTagRule: {
        gatewayId: [
          { required: true, trigger: 'change', message: '请选择网关' }
        ],
        num: [
          { required: true, trigger: 'blur', message: '请输入耳标编号' }
        ]
      },
      // 分页
      pagination: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      // 查询
      FindFromForEarTag: {
        kw: '',
        gatewayIdArr: [],
        gatewayId: ''
      },
      // checked: false,
      dialogVisible: false,
      userRemarkGet: '',
      userList: JSON.parse(sessionStorage.getItem('routerList')),
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      defaultCheckRole: [],
      setRoleIdArrFir: [],
      setRoleIdArrSen: [],
      addNewSwitchData: false
    }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.getEarTagListing()
  },
  // 方法集合
  methods: {
    // 获取开关网关列表
    getEarTagListing () {
      switchGatewayList({
        kw: this.FindFromForEarTag.kw,
        gatewayId: this.FindFromForEarTag.gatewayId,
        limit: this.pagination.pageSize,
        page: this.pagination.page
      }).then(res => {
        if (res.code === '200') {
          this.getGaleWayList = res.data.records
          if (res.data.total > 0 && Math.ceil(res.data.total / this.pagination.size) < this.pagination.page) {
            this.pagination.page = Math.ceil(res.data.total / this.pagination.size)
            this.getEarTagListing()
          } else {
            this.pagination.total = res.data.total
          }
        } else {
          this.$message({
            message: res.message,
            type: 'warning'
          })
        }
      })
    },
    // 打开新增弹框
    addEatTagNow () {
      this.addNewSwitchData = true
    },
    // 新增保存
    commitSwitch () {
      this.$refs.formName.validate((valid) => {
        if (valid) {
          const switchObj = {
            num: this.formForAddSwitchGateway.num
          }
          switchGatewayAdd(switchObj).then(res => {
            this.addNewSwitchData = false
            if (res.code === '200') {
              this.getEarTagListing()
              this.$message({
                message: '操作成功',
                type: 'success'
              })
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
        } else {
          return false
        }
      })
    },
    // 查询
    submitSrearch () {
      this.getEarTagListing()
    },
    // 重置
    resetForm () {
      this.FindFromForEarTag.kw = ''
      this.FindFromForEarTag.gatewayId = ''
    },
    // 删除用户
    deletLiveStockDUser (val) {
      // this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning',
      //     center: true
      //   }).then(() => {
      //     companyDelete(val).then(res => {
      //       if (res.code === '200') {
      //         this.$message({
      //           message: '删除成功',
      //           type: 'success'
      //         })
      //         this.getEarTagListing()
      //       } else {
      //         this.$message({
      //           type: 'info',
      //           message: res,message
      //         });
      //       }
      //     })
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消删除'
      //     });
      //   });
    },
    // 用户详情
    getLiveSwitchDetail (val) {
      this.dialogVisible = true
      switchGatewayDetail(val).then(res => {
        this.updateSwitchDetail = res.data
      })
    },
    // 修改用户详情
    // setConfigUserRole () {
    //   deviceGatewayGatewayList().then(res => {
    //     if (res.code === '200') {
    //       this.updateSwitchDetail.gateWayIdArr = res.data
    //     }
    //   })
    //   this.$confirm('是否确认修改耳标信息', '提示', {
    //     confirmButtonText: '确定',
    //     showCancelButton: '取消',
    //     distinguishCancelAndClose: false
    //   }).then(() => {
    //     this.isDisabled = false
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '取消!'
    //     })
    //   })
    // },
    // // 保存修改后用户信息
    // submitConfigUser () {
    //   this.$refs.deatilsFrom.validate((valid) => {
    //     if (valid) {
    //       const EarTagConfig = {
    //         gatewayId: this.updateSwitchDetail.gatewayId,
    //         num: this.updateSwitchDetail.num,
    //         tagId: this.updateSwitchDetail.id
    //       }
    //       earTagUpdate(EarTagConfig).then(res => {
    //         if (res.code === '200') {
    //           this.$message({
    //             message: '操作成功',
    //             type: 'success'
    //           })
    //           this.dialogVisible = false
    //           this.isDisabled = true
    //           this.getEarTagListing()
    //         } else {
    //           this.$message({
    //             type: 'info',
    //             message: res.message
    //           })
    //         }
    //       })
    //     }
    //   })
    // },
    // 关闭新增弹框
    closeDia () {
      this.formForAddSwitchGateway = {}
    },
    // 关闭详情弹框
    closeDiaDetail () {
      this.dialogVisible = false
      this.isDisabled = true
    }
  }
}
</script>
<style lang='scss' scoped>
  //@import url(); 引入公共css类
.authority {
  width: 100%;
  height: 100%;
}
.fir {
  width: auto;
  height: 30px;
  line-height: 30px;
  span {
    margin-left: 10px;
    padding: 5px;
    width: 30%;
  }
}
.sen {
  margin: 5px auto;
  width: 80%;
  padding: 10px;
  border: 1px solid #f0f0f0;
}
.buttons {
  width: 100%;
  text-align: center;
}
</style>
